﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.ComponentModel;
using System.Collections.Generic;
using System.Windows.Data;
using System.IO;
using System.Linq;
using Cinch;
using MEFedMVVM.ViewModelLocator;
using System.Windows.Threading;

namespace Anata.Client
{
    /// <summary>
    /// </summary>
    [ExportViewModel("DetailWorkspaceViewModel")]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class DetailWorkspaceViewModel : ViewModelBase
    {
        #region Data
		#region Data.Primitives
		#endregion

        #region Data.Rules
		#endregion
        
        #region Data.VMs
        private WorkspaceData transaksiWD;
        private WorkspaceData konsumenWD;
        private WorkspaceData karyawanWD;
        #endregion

        #region Data.Services
        private IMessageBoxService messageBoxService;
        private IUIVisualizerService uiVisualizerService;
        #endregion

        #endregion

        #region Constructor
        [ImportingConstructor]
        public DetailWorkspaceViewModel(
            IMessageBoxService messageBoxService,
            IUIVisualizerService uiVisualizerService)
        {
            #region Constructor.SetupServices
            this.messageBoxService = messageBoxService;
            this.uiVisualizerService = uiVisualizerService;
            #endregion

            #region Constructor.SetupCommands
            #endregion

            #region Constructor.SetupDataWrappers
            #endregion

            #region Constructor.SetupValidationRules
            #endregion

            #region Constructor.SetupVMs
            transaksiWD = new WorkspaceData(@"/Anata.Client;component/Images/About.png",
                    "TransaksiWorkspaceView", null, "Detil Transaksi", true);
            konsumenWD = new WorkspaceData(@"/Anata.Client;component/Images/About.png",
                    "KonsumenWorkspaceView", null, "Detil Konsumen", true);
            karyawanWD = new WorkspaceData(@"/Anata.Client;component/Images/About.png",
                    "KaryawanWorkspaceView", null, "Detil Karyawan", true);
            #endregion

            Mediator.Instance.Register(this);
        }
        #endregion

        #region Public Properties

        #region Public.Commands
        #endregion

        #region Public.ViewMode
        #endregion

        #region Public.Data
        #endregion

        #region Public.CachedCollection
        #endregion

        #region Public.DataVM
        #endregion

        #endregion

        #region Command Handlers
        #endregion

        #region Private Methods
        private bool TryClosePreviousWorkspace()
        {
            bool success = true;
            if (this.Views.Contains(transaksiWD))
            {
                Mediator.Instance.NotifyColleagues<TransaksiThumbnailViewModel>(
                    "TryCloseTransaksiWorkspace",
                    (TransaksiThumbnailViewModel)transaksiWD.DataValue);
                success = (transaksiWD.DataValue == null);
                if (!success)
                {
                    Mediator.Instance.NotifyColleagues<TransaksiThumbnailViewModel>(
                        "TryHighlightTransaksiThumbnail",
                        (TransaksiThumbnailViewModel)transaksiWD.DataValue);
                }
            }
            else if (this.Views.Contains(konsumenWD))
            {
                Mediator.Instance.NotifyColleagues<KonsumenThumbnailViewModel>(
                    "TryCloseKonsumenWorkspace",
                    (KonsumenThumbnailViewModel)konsumenWD.DataValue);
                success = konsumenWD.DataValue == null;
                if (!success)
                {
                    Mediator.Instance.NotifyColleagues<KonsumenThumbnailViewModel>(
                        "TryHighlightKonsumenThumbnail",
                        (KonsumenThumbnailViewModel)konsumenWD.DataValue);
                }
            }
            else if (this.Views.Contains(karyawanWD))
            {
                Mediator.Instance.NotifyColleagues<KaryawanThumbnailViewModel>(
                    "TryCloseKaryawanWorkspace",
                    (KaryawanThumbnailViewModel)karyawanWD.DataValue);
                success = karyawanWD.DataValue == null;
                if (!success)
                {
                    Mediator.Instance.NotifyColleagues<KaryawanThumbnailViewModel>(
                        "TryHighlightKaryawanThumbnail",
                        (KaryawanThumbnailViewModel)karyawanWD.DataValue);
                }
            }
            return success;
        }
        #endregion

        #region Overrides
        #endregion

        #region EditableValidatingViewModelBase overrides
        #endregion

        #region Mediator Message Sinks
        [MediatorMessageSink("TryActivateTransaksiDetail")]
        void TryActivateTransaksiDetail(TransaksiThumbnailViewModel ttvm)
        {
            Console.WriteLine("ada yang panggil");
            if (!ttvm.Equals((TransaksiThumbnailViewModel)transaksiWD.DataValue))
            {
                if (TryClosePreviousWorkspace())
                {
                    transaksiWD.DataValue = ttvm;
                    Views.Add(transaksiWD);
                    SetActiveWorkspace(transaksiWD);
                    Mediator.Instance.NotifyColleagues<TransaksiThumbnailViewModel>(
                        "TryHighlightTransaksiThumbnail", ttvm);
                }
                else
                {
                    //do nothing
                    //else -> tidak jadi lihat detail transaksi
                }
            }
        }
        [MediatorMessageSink("DeactivateTransaksiDetail")]
        void DeactivateTransaksiDetail(TransaksiThumbnailViewModel ttvm)
        {
            if (ttvm.Equals(transaksiWD.DataValue) && this.Views.Contains(transaksiWD))
            {
                this.Views.Remove(transaksiWD);
                transaksiWD.DataValue = null;
            }
        }
        




        [MediatorMessageSink("TryActivateKonsumenDetail")]
        void TryActivateKonsumenDetail(KonsumenThumbnailViewModel ktvm)
        {
            if (!ktvm.Equals((KonsumenThumbnailViewModel)konsumenWD.DataValue))
            {
                if (TryClosePreviousWorkspace())
                {
                    //Mediator.Instance.NotifyColleagues<TransaksiThumbnailViewModel>("LoadTransaksiThumbnailToWorkspace", ttvm);
                    konsumenWD.DataValue = ktvm;
                    Views.Add(konsumenWD);
                    SetActiveWorkspace(konsumenWD);
                    Mediator.Instance.NotifyColleagues<KonsumenThumbnailViewModel>(
                        "TryHighlightKonsumenThumbnail", ktvm);
                }
                else
                {
                    //do nothing
                }
            }
        }
        [MediatorMessageSink("DeactivateKonsumenDetail")]
        void DeactivateKonsumenDetail(KonsumenThumbnailViewModel ktvm)
        {
            if (ktvm.Equals(konsumenWD.DataValue) && this.Views.Contains(konsumenWD))
            {
                this.Views.Remove(konsumenWD);
                konsumenWD.DataValue = null;
            }
        }

        [MediatorMessageSink("TryActivateKaryawanDetail")]
        void TryActivateKaryawanDetail(KaryawanThumbnailViewModel ktvm)
        {
            if (!ktvm.Equals((KaryawanThumbnailViewModel)karyawanWD.DataValue))
            {
                if (TryClosePreviousWorkspace())
                {
                    karyawanWD.DataValue = ktvm;
                    Views.Add(karyawanWD);
                    SetActiveWorkspace(karyawanWD);
                    Mediator.Instance.NotifyColleagues<KaryawanThumbnailViewModel>(
                        "TryHighlightKaryawanThumbnail", ktvm);
                }
                else
                {
                    //do nothing
                }
            }
        }
        [MediatorMessageSink("DeactivateKaryawanDetail")]
        void DeactivateKaryawanDetail(KaryawanThumbnailViewModel ktvm)
        {
            if (ktvm.Equals(karyawanWD.DataValue) && this.Views.Contains(karyawanWD))
            {
                this.Views.Remove(karyawanWD);
                karyawanWD.DataValue = null;
            }
        }
        #endregion
    }

}
